/**
 * 
 */
package com.maoye.ossp.biz.statement.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.biz.brand.entity.Supplier;
import com.maoye.ossp.biz.brand.service.ISupplierService;
import com.maoye.ossp.biz.brand.service.impl.SupplierServiceImpl;
import com.maoye.ossp.biz.statement.entity.BizStatementStmCounter;
import com.maoye.ossp.biz.statement.entity.StatementExpInvoice;
import com.maoye.ossp.biz.statement.service.StatementExpInvoiceService;
import com.maoye.ossp.biz.statement.util.ws.ParamVO;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.util.ConvertUtils;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.utils.SysDictUtils;
import com.maoye.ossp.sys.org.util.UserUtils;

/**
 * 
 * @author xiaohan
 * @date 2014年2月13日
 * @version
 * @filename 
 *           com.maoye.ossp.biz.receipt.controller.SuppliesReceiptController.java
 */
@Controller
@RequestMapping(value = "/biz/statement/exp")
public class StatementExpInvoiceController extends BaseController {

	@Resource
	private StatementExpInvoiceService statementExpInvoiceService;
	
	@Resource
	private ISupplierService supplierService;
	@Resource
	private SupplierServiceImpl supplierServiceImpl;

	// @Resource
	// private ISupplierService supplierService;

	private void invoiceTypeHandler(StatementExpInvoice invoice) {
		if (invoice != null) {
			invoice.setBillType(SysDictUtils.getValue(
					ConvertUtils.ConstantUtils.ST_STATEMENTINVOICE_BILLTYPE,
					invoice.getBillType()));
		}
	}

	private void invoiceTypeHandler(List<StatementExpInvoice> list) {
		if (list != null && list.size() > 0) {
			for (StatementExpInvoice statementInvoice : list) {
				statementInvoice
						.setBillType(SysDictUtils
								.getValue(
										ConvertUtils.ConstantUtils.ST_STATEMENTINVOICE_BILLTYPE,
										statementInvoice.getBillType()));
			}
		}
	}

	@RequestMapping(value = "/list")
	public String list(StatementExpInvoice statementExpInvoice,
			HttpServletRequest request, Model model) {
		
		       // 获取登陆用户Id
				Long userId = UserUtils.getCurrentUserId();
				// 根据当前用户Id 如果是供应商 获取用户获取供应商信息
				Supplier supplier = supplierServiceImpl.get(userId);
				QueryFilter queryFilter = getQuerFilter(request, statementExpInvoice);
				// 设置供应商编码
				if (supplier != null) {
					queryFilter.addFilter("supplierNo", supplier.getSupplierNo(),
							QueryOP.EQUAL);
				}			
		Page<StatementExpInvoice> page = (Page<StatementExpInvoice>) statementExpInvoiceService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("statementExpInvoice", statementExpInvoice);
		return resultPage("/biz/statement/invoice/exp/list", model, request);
	}

	@RequestMapping(value = "/{id}/detail")
	public String getObjByID(@PathVariable Long id, Model model) {
		StatementExpInvoice invoice = statementExpInvoiceService.get(id);
		model.addAttribute("obj", invoice);
		return "/biz/statement/invoice/exp/view";
	}

	@RequestMapping(value = "/{id}/update/view")
	public String toView(@PathVariable Long id, Model model) {
		StatementExpInvoice invoice = statementExpInvoiceService.get(id);
		invoiceTypeHandler(invoice);
		model.addAttribute("obj", invoice);
		return "/biz/statement/invoice/exp/add";
	}

	@RequestMapping(value = "/{id}/confirm")
	public String confirm(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		String msg = "";
		int rtn = statementExpInvoiceService.confirmSXI(id, true);
		if (rtn == 1) {
			msg = "确认成功！";
		} else {
			msg = "确认失败！";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:/biz/statement/exp/" + id + "/detail";
	}

	@RequestMapping(value = "/update")
	@ResponseBody
	public String update(StatementExpInvoice entity,
			RedirectAttributes redirectAttributes) {
		int count = statementExpInvoiceService.updateSupplierReceipt(entity);
		addMessage(redirectAttributes, "修改成功！");
		return ConvertUtils.createResultJson(count == 1);
	}

	@RequestMapping(value = "/{id}/delete")
	public String deleteSR(@PathVariable String id,
			RedirectAttributes redirectAttributes) {
		int count = statementExpInvoiceService.deleteSupplierReceipt(id);
		boolean isSuccess = count == 1;
		addMessage(redirectAttributes, isSuccess ? "删除成功！" : "删除失败！");
		// return "/biz/statement/receipt/exp/list";
		return "redirect:/biz/statement/exp/list";
	}

}
